package com.ja.openchat.adapters.node.section.provider;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.R;
import com.ja.openchat.adapters.node.section.RootFooterNode;

public class RootFooterNodeProvider extends BaseNodeProvider {

    public RootFooterNodeProvider() {
        addChildClickViewIds(R.id.footerTv);

    }

    @Override
    public int getItemViewType() {
        return 2;
    }

    @Override
    public int getLayoutId() {
        return R.layout.node_footer;
    }

    @Override
    public void convert(@NonNull BaseViewHolder helper, @Nullable BaseNode data) {
        RootFooterNode entity = (RootFooterNode) data;
        TextView footerTv = helper.findView(R.id.footerTv);
        footerTv.setText(entity.getTitle());
        if (entity.getTitle().contains(getContext().getString(R.string.expand_text))) {
            footerTv.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.dynamic_down, 0, 0);
        } else {
            footerTv.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.dynamic_up, 0, 0);
        }
    }

    @Override
    public void onChildClick(@NonNull BaseViewHolder helper, @NonNull View view, BaseNode data, int position) {
        if (view.getId() == R.id.footerTv) {
            RootFooterNode entity = (RootFooterNode) data;
            if (entity.getTitle().contains(getContext().getString(R.string.expand_text))) {
                entity.setTitle(getContext().getString(R.string.collapse_reply));
            } else {
                entity.setTitle(getContext().getString(R.string.expand_more_replies));
            }
            getAdapter().notifyItemChanged(position + 1);
            int parentNode = getAdapter().findParentNode(position - 1);
            if (parentNode < 0) {
                getAdapter().expandOrCollapse(position - 1);
            } else {
                getAdapter().expandOrCollapse(parentNode);
            }
        }
    }
}
